//
//  MainViewController.m
//  test_lv
//
//  Created by nykj-mac-03 on 2017/5/18.
//  Copyright © 2017年 renzhi. All rights reserved.
//

#import "MainViewController.h"


@interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MainCollectionCellDelegate>
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UIButton *headButton;
@property (nonatomic, strong) UILabel *nameLable;
@property (nonatomic, strong) UILabel *roleLable;
@property (nonatomic, strong) UILabel *companyLable;
@property (nonatomic, strong) UIView *barView;
@property (nonatomic, strong) UILabel *barTitleLable;
@property (nonatomic, strong) UIButton *barSetButton;
@property (nonatomic, strong) UIButton *barNewsButton;
@property (nonatomic, strong) MainTableView *tableView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *topImage;

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSDictionary *userInfoDic;

@end

@implementation MainViewController
- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
 
    self.userInfoDic = [NSDictionary dictionary];
    
    
    self.titleArray = @[@"律所案件",@"新闻资讯",@"收费审批",@"变更律师",@"财务收案",@"财务结案",@"主任收案",@"主任结案"];
    self.imageArray = @[@"list_05",@"list_06",@"list_sf",@"list_09",@"list_28",@"list_24",@"list_25",@"list_26"];
    [self initalizeAppearance];
//    [self performSelector:@selector(getInfo) withObject:nil afterDelay:1.0f];//延时
    [self getInfo];
//    [self getData];
}
- (void)getInfo{
    NSLog(@"dic = %@",[InfoSingleTon personInfoSingleTon].infoDic);
    self.userInfoDic = [InfoSingleTon personInfoSingleTon].infoDic;
}
- (void)initalizeAppearance{
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.barView];
    [self.barView addSubview:self.barTitleLable];
    [self.barView addSubview:self.barNewsButton];
    
    [self.scrollView addSubview:self.topView];
    [self.scrollView addSubview:self.tableView];
    
    [self.topView addSubview:self.topImage];
    [self.topView addSubview:self.collectionView];
    [self.topView addSubview:self.headImage];
    [self.topView addSubview:self.nameLable];
    [self.topView addSubview:self.roleLable];
    [self.topView addSubview:self.companyLable];
    [self.topView addSubview:self.headButton];
    
    NSArray *titleArr = @[@"我的案件",@"添加案件",@"文     件",@"签到打卡"];
    NSArray *imageArr = @[@"list_01",@"list_02",@"list_03",@"list_04"];
    CGFloat width = (CGFloat)(SCREEN_WIDTH - 150)/4;
    for (int i = 0; i < titleArr.count; i++) {
        [self.view layoutIfNeeded];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15+width*(i%4)+40*(i%4), 170, width, width)];
        [button setImage:[UIImage imageNamed:imageArr[i]] forState:0];

        button.tag = 10+i;
        [button addTarget:self action:@selector(action_FuncButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(15+width*(i%4)+40*(i%4)-3, 220, width+10, 20)];
        titleLable.textColor = [UIColor whiteColor];
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.adjustsFontSizeToFitWidth = YES;
        titleLable.text = titleArr[i];
        titleLable.textAlignment = NSTextAlignmentCenter;

        
        [self.topView addSubview:titleLable];
        [self.topView addSubview:button];
    }
}
#pragma mark -- Method

#pragma mark -- ButtonTarget
- (void)action_FuncButton:(UIButton *)sender{
    if (sender.tag == 10) {
        
        MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
        myCaseVC.caseStyle = @"我的案件";
        myCaseVC.caseStateFlag = 0;
        myCaseVC.create_id = self.userInfoDic[@"user"][@"id"];
        [self.navigationController pushViewController:myCaseVC animated:YES];
    }
    if (sender.tag == 11) {
        AddCaseViewController *addCaseVC = [[AddCaseViewController alloc] init];
        [self.navigationController pushViewController:addCaseVC animated:YES];
    }
    if (sender.tag == 12) {
        FileViewController *fileVC = [[FileViewController alloc] init];
        [self.navigationController pushViewController:fileVC animated:YES];
    }
    if (sender.tag == 13) {
        PunchCardViewController *PCVC = [[PunchCardViewController alloc] init];
        [self.navigationController pushViewController:PCVC animated:YES];
    }
}
- (void)action_SettingButton{
    NSLog(@"设置 ");
}
- (void)action_NewsButton{
    NSLog(@"消息 ");
    MessageViewController *messageVC = [[MessageViewController alloc] init];
    
    [self.navigationController pushViewController:messageVC animated:YES];
}
- (void)action_PushToPersonalInfo{
    UITabBarController *tabb = (UITabBarController *) [UIApplication sharedApplication].keyWindow.rootViewController ;
    tabb.selectedIndex = 3;
}
#pragma mark - MainCollectionCellDelegate
- (void)didClickFuncButtonAction:(UIButton *)sender{
    if (sender.tag == 10) {
        MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
        myCaseVC.caseStyle = @"律所案件";
        myCaseVC.company_id = self.userInfoDic[@"cid"];
        [self.navigationController pushViewController:myCaseVC animated:YES];
    }
    if (sender.tag == 11) {
        NewsViewController *newsVC = [[NewsViewController alloc] init];
        newsVC.accepterId = self.userInfoDic[@"cid"];
        [self.navigationController pushViewController:newsVC animated:YES];
    }
    if (sender.tag == 12) {
        ChargeViewController *chargeVC = [[ChargeViewController alloc] init];
        [self.navigationController pushViewController:chargeVC animated:YES];
    }
    if (sender.tag == 13) {
        NSLog(@"变更律师 ");
        ChangeLawyerViewController *CLVC = [[ChangeLawyerViewController alloc] init];
        [self.navigationController pushViewController:CLVC animated:YES];
    }
    if (sender.tag == 14) {
        FinanceViewController *financelVC = [[FinanceViewController alloc] init];
        financelVC.typeButtonIndex = 0;
        [self.navigationController pushViewController:financelVC animated:YES];
    }
    if (sender.tag == 15) {
        FinanceViewController *financelVC = [[FinanceViewController alloc] init];
        financelVC.typeButtonIndex = 1;
        [self.navigationController pushViewController:financelVC animated:YES];
    }
    if (sender.tag == 16) {
        ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
        approvalVC.typeButtonIndex = 0;
        [self.navigationController pushViewController:approvalVC animated:YES];
    }
    if (sender.tag == 17) {
        ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
        approvalVC.typeButtonIndex = 1;
        [self.navigationController pushViewController:approvalVC animated:YES];
    }
    
}
#pragma mark - ScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point = [scrollView.panGestureRecognizer velocityInView:self.view];
    if (point.y > 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.barView.alpha = 1;
        }];
    }else if (point.y < 0){
        [UIView animateWithDuration:0.5 animations:^{
            self.barView.alpha = 0;
        }];
    }
    CGFloat y = scrollView.contentOffset.y + 20;
    if (y <= 0) {
        CGRect newFrame = self.topView.frame;
        newFrame.origin.y = y;
        self.topView.frame = newFrame;
        
        newFrame = self.tableView.frame;
        newFrame.origin.y = y + 378;
        self.tableView.frame = newFrame;
        
        //偏移量给到tableview，tableview自己来滑动
        self.tableView.contentOffsetY = y;
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    // 松手时判断是否刷新
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 64) {
        [self.tableView startRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView endRefreshing];
        });
    }
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCollectionCell" forIndexPath:indexPath];
    
    cell.titleLable.text = self.titleArray[indexPath.row];
    [cell.funcButton setBackgroundImage :[UIImage imageNamed:self.imageArray[indexPath.row]] forState:0];
//    [cell.funcButton setImage:[UIImage imageNamed:self.imageArray[indexPath.row]] forState:0];
    
    [cell.funcButton layoutButtonWithEdgInsetsStyle:WxyButtonEdgeInsetsStyleImageTop imageTitleSpacing:110];
    cell.funcButton.tag = 10 + indexPath.row;
    cell.delegate = self;
    return cell;
}


#pragma mark -- Getter/Setter


- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    self.barTitleLable.text = title;
}
- (UIView *)barView{
    if (!_barView) {
        _barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)];
        _barView.backgroundColor = [UIColor whiteColor];
    }
    return _barView;
}
- (UILabel *)barTitleLable{
    if (!_barTitleLable) {
        _barTitleLable = [[UILabel alloc]init];
        _barTitleLable.bounds = CGRectMake(0, 0, 200, 20);
        _barTitleLable.center = CGPointMake(self.barView.center.x, self.barView.center.y + 10);
        _barTitleLable.text = @"首页";
        _barTitleLable.textAlignment = NSTextAlignmentCenter;
        _barTitleLable.font = [UIFont systemFontOfSize:18];
        _barTitleLable.textColor = [UIColor blackColor];
    }
    return _barTitleLable;
}
- (UIButton *)barNewsButton{
    if (!_barNewsButton) {
        _barNewsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _barNewsButton.frame = CGRectMake(330, 25, 35, 35);
        [_barNewsButton setBackgroundImage:[UIImage imageNamed:@"smswd"] forState:UIControlStateNormal];
        [_barNewsButton addTarget:self action:@selector(action_NewsButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _barNewsButton;
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _scrollView.contentSize = CGSizeMake(0, self.tableView.bounds.size.height + self.topView.bounds.size.height + self.collectionView.bounds.size.height);
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (MainTableView *)tableView{
    if (!_tableView) {
        _tableView = [[MainTableView alloc] initWithFrame:CGRectMake(0, 378, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        [_tableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"mainTableViewCell"];
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
        

    }
    return _tableView;
}
- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 360)];
        _topView.backgroundColor = [UIColor clearColor];
    }
    return _topView;
}
- (UIImageView *)topImage{
    if (!_topImage) {
        _topImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 252)];
        _topImage.image = [UIImage imageNamed:@"top_bg.jpg"];
        _topImage.backgroundColor = [UIColor clearColor];
    }
    return _topImage;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(80, 60);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 260, SCREEN_WIDTH, 130) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.scrollEnabled = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"mainCollectionCell"];

    }
    return _collectionView;
}
- (UIImageView *)headImage{
    if (!_headImage) {
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(30, 74, 80, 80)];
        _headImage.layer.masksToBounds = YES;
        _headImage.layer.cornerRadius = 40;
        _headImage.layer.borderWidth = 2;
        _headImage.image = [UIImage imageNamed:@"头像"];
        _headImage.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _headImage;
}
- (UILabel *)nameLable{
    if (!_nameLable) {
        _nameLable = [[UILabel alloc] initWithFrame:CGRectMake(140, 70, 200, 50)];
        _nameLable.text = @"Lynn";
        _nameLable.textColor = [UIColor whiteColor];
        _nameLable.font = [UIFont systemFontOfSize:40];
    }
    return _nameLable;
}
- (UILabel *)roleLable{
    if (!_roleLable) {
        _roleLable = [[UILabel alloc] initWithFrame:CGRectMake(140, 130, 100, 30)];
        _roleLable.text = @"最高管理员";
        _roleLable.textColor = [UIColor whiteColor];
        _roleLable.font = [UIFont systemFontOfSize:14];
    }
    return _roleLable;
}
- (UILabel *)companyLable{
    if (!_companyLable) {
        _companyLable = [[UILabel alloc] initWithFrame:CGRectMake(240, 130, 100, 30)];
        _companyLable.text = @"铭轩科技";
        _companyLable.textColor = [UIColor whiteColor];
        _companyLable.textColor = [UIColor whiteColor];
        _companyLable.font = [UIFont systemFontOfSize:14];
    }
    return _companyLable;
}
- (UIButton *)headButton{
    if (!_headButton) {
        _headButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _headButton.frame = CGRectMake(20, 74, 300, 80);
        _headButton.backgroundColor = [UIColor clearColor];
        [_headButton addTarget:self action:@selector(action_PushToPersonalInfo) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headButton;
}



- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
